'''该python代码用以将一长段视频，分割成自己需要的时长段的视频。'''
import numpy as np
import cv2
import os
import time

START_HOUR = 0
START_MIN = 25
START_SECOND = 56
START_TIME = START_HOUR * 3600 + START_MIN * 60 + START_SECOND  # 设置开始时间(单位秒)
END_HOUR = 0
END_MIN = 27
END_SECOND = 23
END_TIME = END_HOUR * 3600 + END_MIN * 60 + END_SECOND  # 设置结束时间(单位秒)

video = "/home/xys/CloundShiProjects/xitieshan/videos/hous_video/1_2522-0517.mp4"
video_save_path = '/home/xys/CloundShiProjects/xitieshan/videos/hous_video/新建文件夹'+'/4.mp4'
cap = cv2.VideoCapture(video)
FPS = cap.get(cv2.CAP_PROP_FPS)
print(FPS)
FPS = 25.0
size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# size = (1920,1080)
print(size)
TOTAL_FRAME = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))  # 获取视频总帧数
frameToStart = START_TIME * FPS  # 开始帧 = 开始时间*帧率
print(frameToStart)
frametoStop = END_TIME * FPS  # 结束帧 = 结束时间*帧率
print(frametoStop)

if video_save_path != "":
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    videoWriter = cv2.VideoWriter(video_save_path, fourcc, FPS, size)
# videoWriter =cv2.VideoWriter('./video/video1.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), FPS, size)
# cap.set(cv2.CAP_PROP_POS_FRAMES, frameToStart)  # 设置读取的位置,从第几帧开始读取视频
COUNT = 0
while True:
        success, frame = cap.read()
        if success:
            COUNT += 1
            if COUNT <= frametoStop and COUNT > frameToStart:  # 选取起始帧
                print('correct= ', COUNT)
                videoWriter.write(frame)
        # print('mistake= ', COUNT)
        if COUNT > frametoStop:
            break
print('end')



# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : 切片.py
# Author: DaShenHan&道长-----先苦后甜，任凭晚风拂柳颜------
# Date  : 2022/3/17
# 自动切片程序,参考 https://www.cnblogs.com/fieldtianye/p/13427303.html
# 需要本地有ffmpeg程序并添加到了环境变量

# import os
#
#
# class self_video:
#     def __init__(self, input, output):
#         self.input = input
#         self.output = output
#         os.makedirs(self.input, exist_ok=True)
#         os.makedirs(self.output, exist_ok=True)
#         os.makedirs(self.output + '/m3u8', exist_ok=True)
#         os.makedirs(self.output + '/ts', exist_ok=True)
#
#     def convert_mp4(self, input, output):
#         input = os.path.abspath(self.input + '/' + input)
#         output = os.path.abspath(self.output + '/' + output)
#         print(input)
#         print(output)
#         if not os.path.exists(input):
#             print('输入文件不存在!!!')
#             return None
#         cmd = f'ffmpeg -i {input} -y -c:v libx264 -strict -2 {output}'
#         print(f'开始执行命令{cmd}')
#         os.system(cmd)
#         print('mp4转换命令执行完毕')
#         return output
#
#     def mp4_to_ts(self, mp4, ts):
#         ts = self.output + '/' + ts
#         cmd = f'ffmpeg -y -i {mp4} -vcodec copy -acodec copy -vbsf h264_mp4toannexb {ts}'
#         print(f'开始执行命令{cmd}')
#         os.system(cmd)
#         print('mp4转换ts命令执行完毕')
#         return ts
#
#     def ts_split(self, ts, time=10):
#         name = ts.split('.')[0].split('/')[-1]
#         m3u8 = f'{self.output}/m3u8/{name}.m3u8'
#         tsp = f'{self.output}/ts/{name}-%04d.ts'
#         cmd = f'ffmpeg -i {ts} -c copy -map 0 -f segment -segment_list {m3u8} -segment_time {time} {tsp}'
#         print(f'开始执行命令{cmd}')
#         os.system(cmd)
#         print('ts切割多段执行完毕')
#         return m3u8
#
#
# if __name__ == '__main__':
#     qp = self_video('input', 'output')
#     output = qp.convert_mp4('1.mp4', '2.mp4')
#     if output:
#         ts = qp.mp4_to_ts(output, '1.ts')
#         qp.ts_split(ts, 10)
